package com.newaim.util;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtils {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    public void add(String key, Object value) {
        redisTemplate.opsForValue().set(key,value);
    }

    public void add(String key, Object value, Integer minute) {
        redisTemplate.opsForValue().set(key,value,minute, TimeUnit.MINUTES);
    }

    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    public void delete(String key) {
        redisTemplate.delete(key);
    }

    public void putHash(String key, String hashKey, Object obj) {
        redisTemplate.opsForHash().put(key, hashKey, obj);
    }

    public Object getHash(String key, String hashKey) {
        return redisTemplate.opsForHash().get(key, hashKey);
    }

    public Map<Object, Object> getHash(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    public boolean isExist(String key) {
        return redisTemplate.hasKey(key);
    }

    public boolean isExistHashKey(String key, Object hashKey) {
        return redisTemplate.opsForHash().hasKey(key, hashKey);
    }

}
